<template>
    <div>
        <el-divider content-position='left'>
            <el-popover
                ref='popover'
                placement="bottom-start"
                width="500"
                trigger="click"
                @hide='resetCreateFrom'>
                <el-form label-width="120px">
                    <el-form-item label='角色名称'>
                        <el-input v-model='createForm.title'></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type='primary' @click='createConfirm'>确认</el-button>
                    </el-form-item>
                </el-form>
                <el-button type='primary' slot='reference' >添加角色</el-button>
            </el-popover>
        </el-divider>
        <el-table
            :data="tableData"
            stripe
            border
            :header-cell-style="TABLE_HEADER_CELL_STYLE"
            v-loading = "isLoading">
            <el-table-column prop='title' label='角色名称'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='title' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='created_at' label='创建时间'></el-table-column>
            <el-table-column prop='created_at' label='状态'>
                <template v-slot='scope'>
                    <span :class="{'red-text' : !scope.row.permissions || !scope.row.permissions.length}">
                        {{ !scope.row.permissions || scope.row.permissions.length ? '已设置角色权限' : '未设置角色权限'}}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label='操作'>
                <template v-slot='scope'>
                    <el-popconfirm title="确认删除吗？" @confirm='deleteHandle(scope.row)' class="popconfirm">
                        <el-button type='text' slot="reference">删除</el-button>
                    </el-popconfirm>
                    <el-button type='text' @click='toDetail(scope.row)'>配置角色权限</el-button>
                </template>
            </el-table-column>
        </el-table>
        <o-pagination :total='total' @current-change='currentChange' :page-size='queryForm.limit'></o-pagination>
    </div>
</template>

<script>
    import {toast, alert} from '@u'
    export default {
        data () {
            return {
                tableData : [],
                queryForm : {
                    page : 1,
                    limit : 10
                },
                total : 0,
                history : [0],//查看下级菜单的历史
                isLoading : false,
                createForm : {
                    title : null,
                }
            }
        },
        created () {
            this.getTableData();
        },
        methods : {
            getTableData () {
                this.isLoading = true;
                this.__api.system.rolesList(this.queryForm).then(res => {
                    this.isLoading = false;
                    this.total = res.total;
                    this.tableData = res.data;
                })
            },
            //删除
            deleteHandle (row) {
                this.__api.system.rolesDelete({
                    id : row.id
                }).then(_ => {
                    toast('操作成功');
                    this.getTableData();
                })
            },
            //更新
            updateHandle (row) {
                this.$nextTick(_ => {
                    this.__api.system.rolesUpdate(row).then(_ => {
                        toast('操作成功')
                    })
                })
            },
            //创建确认
            createConfirm () {
                if (!this.createForm.title) {
                    toast('请填写角色名称', 'error');
                    return;
                }
                this.__api.system.rolesCreate(this.createForm).then(_ => {
                    toast('操作成功');
                    this.$refs.popover.doClose();
                    this.getTableData();
                    this.resetCreateFrom();
                })
            },
            //充值创建表单
            resetCreateFrom () {
                this.createForm.title = null;
            },
            currentChange (page) {
                this.queryForm.page = page;
                this.getTableData();
            },
            //角色详情
            toDetail (row) {
                this.$router.push({
                    path : `/system/role-detail/${row.id}`,
                    query : {
                        info : JSON.stringify(row)
                    }
                })
            }
        }
    }
</script>

<style>

</style>